home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / gcctest / tests03.zoo / tstdio.c < prev    next >
C/C++ Source or Header  |  1992-04-27  |  18KB  |  957 lines

  1. /*            E x e r c i s e
  2.  *
  3.  * This program exercises the stdio routines. It does not validate the
  4.  * routines but provides a convenient way to quickly check the functionality
  5.  * of the code.
  6.  */
  7.  
  8. #ifdef _BSD
  9. # include <strings.h>
  10. # define remove unlink
  11. # define sleep(x)    usleep((x)*500)
  12. #else
  13. # include <stdlib.h>
  14. # include <string.h>
  15. #endif
  16. #include <stdio.h>
  17.  
  18. #ifdef atarist
  19. #include <memory.h>
  20. #include <unistd.h>
  21. #endif
  22.  
  23. #ifndef SEEK_SET
  24. /* lseek() origins */
  25. #define    SEEK_SET    0        /* from beginning of file */
  26. #define    SEEK_CUR    1        /* from current location */
  27. #define    SEEK_END    2        /* from end of file */
  28. #endif
  29.  
  30. #define UCHAR(x)    ((int) ((x) & 0xff))
  31.  
  32. #define DEC -123
  33. #define INT 255
  34. #define UNS (~0)
  35. #define TESTFILE    "test.dat"
  36. #define LARGEBUFS    16
  37. #ifdef        MSDOS
  38. # define    TTY    "con"
  39. #else
  40. # ifdef atarist
  41. #  define    TTY    "CON:"
  42. # else
  43. #  define    TTY    "/dev/tty"
  44. # endif
  45. #endif
  46.  
  47. #ifndef atarist
  48. extern void *malloc();            /* memory allocator */
  49. extern char *strcpy();            /* string copy */
  50. extern char *strcat();            /* string concatenation */
  51. extern int strcmp();            /* string compare */
  52. extern void exit();            /* exit */
  53. #endif
  54.  
  55. FILE *fp;                /* per test file pointer */
  56.  
  57. /*
  58.  * Line Buffered Write Test
  59.  *
  60.  * Write to a terminal. This tests that the output buffer is
  61.  * flushed on receipt of a \n.
  62.  */
  63.  
  64. void lbw_test()
  65.  
  66. {
  67.   int i;
  68.  
  69.   puts("\nLine buffered write test");
  70. #ifdef atarist
  71.   if ((fp = fopen(TTY, "wt")) != NULL) {
  72. #else
  73.   if ((fp = fopen(TTY, "w")) != NULL) {
  74. #endif
  75.     puts("<pause>ABCDEFGH");
  76.     puts("<pause>ABCD<pause>EFGH");
  77.     for (i = 0; i < 8; i++)
  78.       putc('A'+i, fp), sleep(1);
  79.     putc('\n', fp);
  80.     for (i = 0; i < 8; i++) {
  81.       putc('A'+i, fp);
  82.       if (i == 3)
  83.     fflush(fp);
  84.       sleep(1);
  85.     }
  86.     fclose(fp);
  87.     puts("");
  88.   }
  89. }
  90.  
  91. /*
  92.  * Unbuffered Write Test
  93.  *
  94.  * Test that characters are written directly to the output device
  95.  * when the stream is unbuffered.
  96.  */
  97.  
  98. void ubw_test()
  99.  
  100. {
  101.   int i;
  102.  
  103.   puts("\nUnbuffered write test");
  104. #ifdef atarist
  105.   if ((fp = fopen(TTY, "wt")) != NULL) {
  106. #else
  107.   if ((fp = fopen(TTY, "w")) != NULL) {
  108. #endif
  109.     setbuf(fp, (char *) 0);
  110.     puts("A<pause>B<pause>C<pause>D<pause>E<pause>F<pause>G<pause>H<pause>");
  111.     puts("A<pause>B<pause>C<pause>D<pause>E<pause>F<pause>G<pause>H<pause>");
  112.     for (i = 0; i < 8; i++)
  113.       putc('A'+i, fp), sleep(1);
  114.     putc('\n', fp);
  115.     for (i = 0; i < 8; i++)
  116.       putc('A'+i, fp), sleep(1);
  117.     fclose(fp);
  118.     puts("");
  119.   }
  120. }
  121.  
  122. /*
  123.  * Buffered Write Test
  124.  *
  125.  * Test that the data is written to the terminal on a per buffer
  126.  * basis.
  127.  */
  128.  
  129. void bw_test()
  130.  
  131. {
  132.   int i;
  133.  
  134.   puts("\nFully buffered write test");
  135. #ifdef atarist
  136.   if ((fp = fopen(TTY, "wt")) != NULL) {
  137. #else
  138.   if ((fp = fopen(TTY, "w")) != NULL) {
  139. #endif
  140.     setvbuf(fp, (char *) 0, _IOFBF, 4);
  141.     puts("<pause>ABCD<pause>EFGH<pause>");
  142.     puts("ABC<pause>DEFG<pause>H");
  143.     for (i = 0; i < 8; i++)
  144.       putc('A'+i, fp), sleep(1);
  145.     putc('\n', fp);
  146.     for (i = 0; i < 8; i++)
  147.       putc('A'+i, fp), sleep(1);
  148.     fclose(fp);
  149.     puts("");
  150.   }
  151. }
  152.  
  153. /* Formatted Output Test
  154.  *
  155.  * This exercises the output formatting code.
  156.  */
  157.  
  158. void fp_test()
  159.  
  160. {
  161.   int i, j, k, l;
  162.   char buf[7];
  163.   char *prefix = buf;
  164.   char tp[20];
  165.  
  166.   puts("\nFormatted output test");
  167.   printf("prefix  6d      6o      6x      6X      6u\n");
  168.   strcpy(prefix, "%");
  169.   for (i = 0; i < 2; i++) {
  170.     for (j = 0; j < 2; j++) {
  171.       for (k = 0; k < 2; k++) {
  172.     for (l = 0; l < 2; l++) {
  173.       strcpy(prefix, "%");
  174.       if (i == 0) strcat(prefix, "-");
  175.       if (j == 0) strcat(prefix, "+");
  176.       if (k == 0) strcat(prefix, "#");
  177.       if (l == 0) strcat(prefix, "0");
  178.       printf("%5s |", prefix);
  179.       strcpy(tp, prefix);
  180.       strcat(tp, "6d |");
  181.       printf(tp, DEC);
  182.       strcpy(tp, prefix);
  183.       strcat(tp, "6o |");
  184.       printf(tp, INT);
  185.       strcpy(tp, prefix);
  186.       strcat(tp, "6x |");
  187.       printf(tp, INT);
  188.       strcpy(tp, prefix);
  189.       strcat(tp, "6X |");
  190.       printf(tp, INT);
  191.       strcpy(tp, prefix);
  192.       strcat(tp, "6u |");
  193.       printf(tp, UNS);
  194.       printf("\n");
  195.     }
  196.       }
  197.     }
  198.   }
  199. }
  200.  
  201. /*
  202.  * String Output Test
  203.  *
  204.  * Test the string printf code.
  205.  */
  206.  
  207. void sw_test()
  208.  
  209. {
  210.   int i;
  211.   char buf[80];
  212.  
  213.   puts("\nTest sprintf functionality");
  214.   puts("13 bytes in 'Testing 1 2 3'");
  215.   i = sprintf(buf, "Testing %d %d %d", 1, 2, 3);
  216.   printf("%d bytes in '%s'\n", i, buf);
  217. }
  218.  
  219. /*
  220.  * String Input Test
  221.  *
  222.  * Test the string scanf code.
  223.  */
  224.  
  225. void sr_test()
  226.  
  227. {
  228.   int i, j;
  229.   char buf[80];
  230.  
  231.   puts("\nTest sscanf functionality");
  232.   puts("2 items yielding 25 and 'thompson'");
  233.   i = sscanf("25 thompson", "%d%s", &j, buf);
  234.   printf("%d items yielding %d and '%s'\n", i, j, buf);
  235. }
  236.  
  237. /*
  238.  * File Write and Read Test
  239.  *
  240.  * Test that a file can be written to and read from.
  241.  */
  242.  
  243. void frw_test()
  244.  
  245. {
  246.   int i, j, k;
  247.   char buf[80];
  248.  
  249.   puts("\nFile write and read check");
  250. #ifdef atarist
  251.   if ((fp = fopen(TESTFILE, "wt")) != NULL) {
  252. #else
  253.   if ((fp = fopen(TESTFILE, "w")) != NULL) {
  254. #endif
  255.     puts("3 items yielding 56, 789 and '56'");
  256.     puts("1 item yielding 'a72'");
  257.     fprintf(fp, "56789 0123 56a72");
  258. #ifdef atarist
  259.     if (freopen(TESTFILE, "rt", fp) != fp)
  260. #else
  261.     if (freopen(TESTFILE, "r", fp) != fp)
  262. #endif
  263.       puts("Cannot open file for reading");
  264.     else {
  265.       i = fscanf(fp, "%2d%d%*d %[0-9]", &j, &k, buf);
  266.       printf("%d items yielding %d, %d and '%s'\n", i, j, k, buf);
  267.       i = fscanf(fp, "%s", buf);
  268.       printf("%d item yielding '%s'\n", i, buf);
  269.       fclose(fp);
  270.     }
  271.   }
  272. }
  273.  
  274. /*
  275.  * File Seek Test
  276.  *
  277.  * Test that seek operations within files work.
  278.  */
  279.  
  280. void fs_test()
  281.  
  282. {
  283.   int i, j;
  284.  
  285.   puts("\nFile seek test");
  286. #ifdef atarist
  287.   if ((fp = fopen(TESTFILE, "wb")) != NULL) {
  288. #else
  289.   if ((fp = fopen(TESTFILE, "w")) != NULL) {
  290. #endif
  291.     for (i = 0; i < 256; i++)
  292.       putc(i, fp);
  293. #ifdef atarist
  294.     if (freopen(TESTFILE, "rb", fp) != fp)
  295. #else
  296.     if (freopen(TESTFILE, "r", fp) != fp)
  297. #endif
  298.       puts("Cannot open file for reading");
  299.     else {
  300.       for (i = 1; i <= 255; i++) {
  301.         printf("\r%3d ", i);
  302.     fflush(stdout);
  303.         fseek(fp, (long) -i, SEEK_END);
  304.     if ((j = getc(fp)) != 256-i) {
  305.       printf("SEEK_END failed %d\n", j);
  306.       break;
  307.     }
  308.     if (fseek(fp, (long) i, SEEK_SET)) {
  309.       puts("Cannot SEEK_SET");
  310.       break;
  311.     }
  312.     if ((j = getc(fp)) != i) {
  313.       printf("SEEK_SET failed %d\n", j);
  314.       break;
  315.     }
  316.     if (fseek(fp, (long) i, SEEK_SET)) {
  317.       puts("Cannot SEEK_SET");
  318.       break;
  319.     }
  320.     if (fseek(fp, (long) (i >= 128 ? -128 : 128), SEEK_CUR)) {
  321.       puts("Cannot SEEK_CUR");
  322.       break;
  323.     }
  324.     if ((j = getc(fp)) != (i >= 128 ? i-128 : i+128)) {
  325.       printf("SEEK_CUR failed %d\n", j);
  326.       break;
  327.     }
  328.       }
  329.       if (i > 255)
  330.     puts("ok");
  331.       fclose(fp);
  332.     }
  333.   }
  334. }
  335.  
  336. /*
  337.  * Test gets()
  338.  *
  339.  * Checks that gets() works.
  340.  */
  341.  
  342. void gets_test()
  343.  
  344. {
  345.   char buf[80];
  346.  
  347.   puts("\nGets functionality");
  348.   puts("... Type a line and have it echoed ...");
  349.   gets(buf);
  350.   puts(buf);
  351. }
  352.  
  353. /*
  354.  * Fputs Test
  355.  *
  356.  * Check that fputs() works into unbuffered streams.
  357.  */
  358.  
  359. void fputs_test()
  360.  
  361. {
  362.   puts("\nUnbuffered fputs test");
  363. #ifdef atarist
  364.   if ((fp = fopen(TTY, "wt")) != NULL) {
  365. #else
  366.   if ((fp = fopen(TTY, "w")) != NULL) {
  367. #endif
  368.     setbuf(fp, (char *) 0);
  369.     puts("ABCDEFGH<pause>");
  370.     puts("ABCDEFGH<pause>");
  371.     fputs("ABCDEFGH", fp), sleep(1);
  372.     fputs("\nABCDEFGH", fp), sleep(1);
  373.     fclose(fp);
  374.     puts("");
  375.   }
  376. }
  377.  
  378. /*
  379.  * Fprintf Test
  380.  *
  381.  * Check that fprintf() works into unbuffered streams.
  382.  */
  383.  
  384. void fprint_test()
  385.  
  386. {
  387.   puts("\nUnbuffered fprintf test");
  388. #ifdef atarist
  389.   if ((fp = fopen(TTY, "wt")) != NULL) {
  390. #else
  391.   if ((fp = fopen(TTY, "w")) != NULL) {
  392. #endif
  393.     setbuf(fp, (char *) 0);
  394.     puts("ABCDEFGH<pause>");
  395.     puts("ABCDEFGH<pause>");
  396.     fprintf(fp, "ABCDEFGH"), sleep(1);
  397.     fprintf(fp, "\nABCDEFGH"), sleep(1);
  398.     fclose(fp);
  399.     puts("");
  400.   }
  401. }
  402.  
  403. /*
  404.  * Fgets Test
  405.  *
  406.  * Check that fgets() works.
  407.  */
  408.  
  409. void fgets_test()
  410.  
  411. {
  412.   char buf[80];
  413.  
  414.   puts("\nFgets functionality");
  415.   puts("a");
  416.   puts("<pause>ab");
  417.   puts("<pause>abc");
  418.   puts("<pause>abcd");
  419.   puts("<pause>abcde");
  420.   puts("<pause>abcdef");
  421.   puts("<pause>abcdefg<pause>");
  422.   puts("<pause>abcdefg<pause>h");
  423. #ifdef atarist
  424.   if ((fp = fopen(TESTFILE, "wt")) != NULL) {
  425. #else
  426.   if ((fp = fopen(TESTFILE, "w")) != NULL) {
  427. #endif
  428.     fputs("a\n", fp);
  429.     fputs("ab\n", fp);
  430.     fputs("abc\n", fp);
  431.     fputs("abcd\n",